// pages/petList/adopt/adopt.js
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    petList: [],
    page: 1,
    length: 1,
    petList: [],
    petType: 1,
    releaseType: ''
  },
  queryCenterPets: function (e) {
    let that = this;
    let petType = e.currentTarget.dataset.petType;
    that.setData({
      petType: petType,
      petList: []
    });
    this.queryPetList();
  },
  queryPetList: function () {
    let that = this;
    let data = 'releaseType=' + that.data.releaseType+'&petType=' + that.data.petType + '&pageNum=' + that.data.page + '&pageSize=10'
    wx.showToast({
      title: '加载中！',
      icon: 'loading',
      duration: 1000
    });
    let fn = res => {
      let _list = that.data.petList;
      for (var i = 0; i < res.data.data.length; i++) {
        _list.push(res.data.data[i]);
      }
      if (that.data.page == 1) {
        _list = res.data.data;
      }
      that.setData({
        petList: _list,
        page: _list.length >= 10 ? 1 : Math.floor(_list.length / 10) + 1,
        length: res.data.data.length
      })
    }
    app.ajax('/petLost/list?' + data, 'get', '', fn)
  },
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    //模拟加载
    this.queryPetList();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  //加载更多
  onReachBottom: function () {
    let that = this;
    if (that.data.length < 10) {
      wx.showToast({
        title: '没有更多了',
        icon: 'none'
      })
      return false;
    } else {
      wx.showToast({
        title: '加载中！',
        icon: 'loading',
        duration: 1000
      });
    }
    this.queryPetList();
  },
  onLoad: function (options) {
    let that = this;
    let _releaseType = options.releaseType;
    that.setData({
      releaseType: _releaseType
    });
    //调用应用实例的方法获取全局数据
    wx.showLoading({
      title: '加载中',
    });
    that.queryPetList();
  },
  petDetail: function (e) {
    var petId = e.currentTarget.dataset.petId;
    wx.navigateTo({
      url: '/pages/petList/lostdetail/lostdetail?petId=' + petId
    })
  },
})